<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账号设置">
            <!-- 放置内容 -->
            <el-form
              ref="userForm"
              :rules="rules"
              :model="userInfo"
              label-width="120px"
              style="margin-left: 120px; margin-top: 30px"
            >
              <el-form-item label="姓名" prop="username">
                <el-input
                  v-model="userInfo.username"
                  style="width: 300px"
                ></el-input>
              </el-form-item>
              <el-form-item label="密码" prop="password2">
                <el-input
                  v-model="userInfo.password2"
                  style="width: 300px"
                  type="password"
                ></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="saveUser">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <el-row type="flex" justify="end">
              <el-tooltip content="打印个人基本信息">
                <router-link :to="`/employees/print/${userId}?type=personal`">
                  <i class="el-icon-printer"></i>
                </router-link>
              </el-tooltip>
            </el-row>
            <!-- 个人详情内容 -->
            <!-- 动态组件 可以切换组件 is必须是变量 -->
            <component :is="UserComponent" />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <el-row type="flex" justify="end">
              <el-tooltip content="打印岗位信息">
                <router-link :to="`/employees/print/${userId}?type=job`">
                  <i class="el-icon-printer"></i>
                </router-link>
              </el-tooltip>
            </el-row>
            <!-- 放置内容 -->
            <!-- <job-info /> -->
            <component :is="JobComponent" />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserDetailById } from "@/api/user";
import { getPersonalDetail, saveUserDetailById, updatePersonal } from "@/api/employees";
// 个人详情
import userInfo from "./components/user-info";
// 岗位信息
import JobInfo from "./components/job-info.vue"
// 部门负责人
import { getEmployeeSimple } from '@/api/departments';
export default {
  components: {
    userInfo, JobInfo
  },
  data() {
    return {
      UserComponent: 'user-Info',
      JobComponent: 'job-info',
      userId: this.$route.params.id,
      userInfo: {
        username: "",
        password2: "", // 为什么叫password2 因为读取出来的password是密文
      },
      rules: {
        username: [
          { required: true, message: "姓名不能为空", trigger: "blur" },
        ],
        password2: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          { min: 6, max: 16, message: "密码长度6-9位", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    // 登录账户设置
    this.getUserDetailById();
    // 用户个人详情
    this.getPersonalDetail();
    // 岗位信息
    this.getJobDetail();
    this.getEmployeeSimple()
  },
  methods: {
    async getUserDetailById() {
      this.userInfo = await getUserDetailById(this.userId);
      console.log(this.userInfo); // object
    },
    // 保存修改信息
    async saveUser() {
      try {
        await this.$refs.userForm.validate();
        await saveUserDetailById({
          ...this.userInfo,
          password: this.userInfo.password2,
        });
        this.$message.success("修改用户信息成功");
      } catch (error) {
        console.log(error);
      }
    },
    async getPersonalDetail(){
        this.formData = await getPersonalDetail(this.userId)
    },
    async savePersonal(){
        await updatePersonal({...this.formData, id:this.userId});
        this.$message.success('保存成功')
    },
    async getJobDetail(){
        this.formData  = await this.getJobDetail(this.userId)
    },
    async getEmployeeSimple(){
        this.depts = await getEmployeeSimple()
    },
    async saveJob(){
        await updateJob({...this.formData, userId:this.userId});
        this.$message.success('保存岗位信息成功')
    }
  },
};
</script>

<style scoped>
</style>